﻿using PLCS.Domain.Shared.Enums;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using static NPOI.HSSF.Util.HSSFColor;

namespace PLCS.App.Shared.Views.UserControls.Chlidren
{
    /// <summary>
    /// Pipeline.xaml 的交互逻辑
    /// </summary>
    public partial class Pipeline : UserControl
    {
        //竖放标志
        private bool verticalflag;

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(Pipeline), new UIPropertyMetadata(null));

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation,styling,binding,etc...
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(Pipeline), new UIPropertyMetadata(null));

        /// <summary>
        /// 强制输入事件和属性来自FrameworkElement
        /// </summary>
        public IInputElement CommandTarget
        {
            get { return (IInputElement)GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandTarget.  This enables animation,etc...
        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(Pipeline), new UIPropertyMetadata(null));

        /// <summary>
        /// 名称
        /// </summary>
        public string SName
        {
            get { return (string)GetValue(SNameProperty); }
            set { SetValue(SNameProperty, value); }
        }

        public static readonly DependencyProperty SNameProperty =
            DependencyProperty.Register("SName", typeof(string), typeof(Pipeline), new PropertyMetadata("S101"));

        public int StationCount
        {
            get { return (int)GetValue(StationCountProperty); }
            set { SetValue(StationCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StationCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StationCountProperty =
            DependencyProperty.Register("StationCount", typeof(int), typeof(Pipeline), new PropertyMetadata(6));

        public Pipeline()
        {
            InitializeComponent();
            SizeChanged += ConveyorLine_SizeChanged;
        }

        /// <summary>
        /// 重载鼠标左击时触发事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            //_eventAggregator.GetEvent<StationEvent>().Publish(SName);
            var command = Command;
            var parameter = CommandParameter;
            var target = CommandTarget;

            var routedCmd = command as RoutedCommand;
            if (routedCmd != null && routedCmd.CanExecute(parameter, target))
            {
                routedCmd.Execute(parameter, target);
            }
            else if (command != null && command.CanExecute(parameter))
            {
                command.Execute(parameter);
            }
        }

        private void ConveyorLine_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Cursor = Cursors.Hand; //小手
            ToolTip = $"输送线:{SName}";
            rotate.Angle = ActualWidth < ActualHeight ? 90 : 0;
        }
    }
}